<template>
  <div class="purchase-list-layout">
    <crumbList :navList="navList"></crumbList>
    <div class="purchase-content">
      <div class="purchase-table">
        <div class="purchase-item">
          <el-form ref="searchData" :model="searchData" class="clearfix">
            <div class="fl">
              <el-form-item>
                <el-select v-model="searchData.factoryName " placeholder="厂商">
                  <el-option :label="item.factoryName" :key="index" :value="item.factoryName"
                             v-for="(item,index) in factoryMsg"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item class="itemTime">
                <el-date-picker type="daterange" placeholder="回收时间" v-model="searchData.choseTime"></el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-input v-model="searchData.fixedNumber" placeholder="设备固号"></el-input>
              </el-form-item>

              <el-form-item>
                <el-input v-model="searchData.recoverMan" placeholder="交接人"></el-input>
              </el-form-item>
              <el-form-item>
                <el-input v-model="searchData.chargeMan" placeholder="负责人"></el-input>
              </el-form-item>
              <el-form-item>
                <el-input v-model="searchData.repairsNum" placeholder="报修次数"></el-input>
              </el-form-item>
            </div>
            <div class="fr">
              <el-form-item class="itemButton">
                <el-button type="primary" @click="submitSearch">查询</el-button>
                <el-button type="primary" @click="resetItem">重置</el-button>
                <el-button type="primary" class="addItem" @click="addList">新增回收信息</el-button>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="purchase-table-list">
          <el-table :data="tableData" border>
            <el-table-column :label="item.label" :width="item.name==='orderNum'?64:''" v-for="(item,index) in tableItem"
                             :key="index">
              <template scope="scope">
                <el-popover trigger="hover" placement="top">
                  <p>{{item.label}}: {{ scope.row[item.name]}}</p>
                  <div slot="reference">
                    <el-tag>{{ scope.row[item.name] }}</el-tag>
                  </div>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="155">
              <template scope="scope">
                <el-button type="primary" class="editBtn" @click="modification(scope.$index, scope.row)">修改</el-button>
                <el-button type="primary" class="deleteBtn" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="block">
          <el-pagination layout="total, prev, pager, next, jumper"
                         @current-change="handleCurrentChange"
                         :current-page="page.pageNum"
                         :page-size="page.pageSize"
                         :total="page.total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import crumbList from '../../common/crumbList.vue'
  import getParam from '../../../utils/getParams/getParams'
  import ajax from '../../../utils/ajax/index'
  import resetDate from '../../../utils/date/resetDate'
  import { mapState } from 'vuex'

  export default {
    data () {
      return {
        navList: [
          {name: '项目详情', path: ''},
          {name: '机具回收', path: 'recycle'},
          {name: JSON.parse(sessionStorage.getItem('projectItemData')).name, path: ''}
        ],
        searchData: {
          factoryName: '',
          choseTime: '',
          fixedNumber: '',
          recoverMan: '',
          chargeMan: '',
          repairsNum: ''
        },
        tableData: [],
        tableItem: [
          {label: '序号', name: 'orderNum'},
          {label: '厂商', name: 'factoryName'},
          {label: '型号', name: 'productType'},
          {label: '回收时间', name: 'recoverTime'},
          {label: '批次', name: 'batch'},
          {label: '设备固号', name: 'fixedNumber'},
          {label: '报修次数', name: 'repairCount'},
          {label: '使用时长', name: 'useTime'},
          {label: '交接人', name: 'recoverMan'},
          {label: '负责人', name: 'chargeMan'},
          {label: '备注', name: 'remark'}
        ],
        page: {
          pageNum: 1,
          pageSize: 1,
          total: 1,
          pages: 1,
          size: 1
        }
      }
    },
    computed: {
      ...mapState(['factoryMsg'])
    },
    components: {crumbList},
    created () {
      let itemData = JSON.parse(sessionStorage.getItem('recycle'))
      if (itemData) {
        sessionStorage.removeItem('recycle')
      }
      // 表单
      this.getFormMes()
    },
    methods: {
      // 获取表单数据
      getFormMes () {
        let url = this.api2.recycle.projectData
        let params = {
          projectCode: getParam('id')
        }
        ajax.get(url, params).then((res) => {
          if (res.msg.code === '20000') {
            let data = res.data
            this.page = data.page
            for (let i = 0; i < data.list.length; i++) {
              data.list[i].edit = true
              data.list[i].orderNum = i + 1
            }
            this.tableData = data.list
          } else {
            this.$message.error(res.msg.message)
          }
        })
      },
      // 查询
      submitSearch () {
        this.searchData.recoverTime = resetDate(this.searchData.choseTime[0])
        this.searchData.endTime = resetDate(this.searchData.choseTime[1])
        let url = this.api2.recycle.projectData
        let params = this.searchData
        params.projectCode = getParam('id')
        ajax.get(url, params).then((res) => {
          if (res.msg.code === '20000') {
            let data = res.data
            this.page = data.page
            for (let i = 0; i < data.list.length; i++) {
              data.list[i].edit = true
              data.list[i].orderNum = i + 1
            }
            this.tableData = data.list
          } else {
            this.$message.error(res.msg.message)
          }
        })
      },
      resetItem () {
        this.searchData = {
          factoryName: '',
          choseTime: '',
          fixedNumber: '',
          recoverMan: '',
          chargeMan: '',
          repairsNum: ''
        }
        this.getFormMes()
      },
      // 新增
      addList () {
        this.$router.push(`/newRecycle?id=` + getParam('id'))
      },
      // 修改
      modification (index, row) {
        sessionStorage.setItem('recycle', JSON.stringify(row))
        this.$router.push(`/newRecycle?id=` + getParam('id'))
      },
      // 删除
      handleDelete (index, row) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {id: row.id}
          let url = this.api2.recycle.deleteProjectData
          ajax.post(url, params).then((res) => {
            if (res.msg.code === '20000') {
              this.$message({type: 'success', message: '删除成功!'})
              this.getFormMes()
            } else {
              this.$message.error(res.msg.message)
            }
          })
        }).catch(() => {
          this.$message({type: 'info', message: '已取消删除'})
        })
      },
      handleCurrentChange (val) {
        this.searchData.recoverTime = resetDate(this.searchData.choseTime[0])
        this.searchData.endTime = resetDate(this.searchData.choseTime[1])
        let url = this.api2.recycle.projectData
        let params = this.searchData
        params.projectCode = getParam('id')
        params.pageNum = val
        ajax.get(url, params).then((res) => {
          if (res.msg.code === '20000') {
            let data = res.data
            this.page = data.page
            for (let i = 0; i < data.list.length; i++) {
              data.list[i].edit = true
              data.list[i].orderNum = i + 1
            }
            this.tableData = data.list
          } else {
            this.$message.error(res.msg.message)
          }
        })
      }
    }
  }
</script>
